package com.yuan.hospital.controller.api;

import com.yuan.common.helper.HttpRequestHelper;
import com.yuan.common.utils.MD5;
import com.yuan.hospital.common.exception.EduException;
import com.yuan.hospital.common.result.Result;
import com.yuan.hospital.common.result.ResultCodeEnum;
import com.yuan.hospital.service.DepartmentService;
import com.yuan.hospital.service.HospitalService;
import com.yuan.hospital.service.HospitalSetService;
import com.yuan.hospital.service.ScheduleService;
import com.yuan.model.hospital.Department;
import com.yuan.model.hospital.Hospital;
import com.yuan.model.hospital.Schedule;
import com.yuan.vo.hosp.DepartmentQueryVo;
import com.yuan.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 医院科室及排班管理
 *
 * @Author yuan
 * @Date 2021/7/15 11:34
 */
@Api(tags = "医院科室及排班管理")
@RestController
@RequestMapping("/api/hosp")
@Slf4j
public class ApiController {
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    HospitalSetService hospitalSetService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private ScheduleService scheduleService;

    /**
     * 删除排班信息
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "删除排班信息")
    @PostMapping("/schedule/remove")
    public Result remove(HttpServletRequest request) {
        //获取传递过来科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //获取医院编号和排班编号
        String hosCode = (String) paramMap.get("hoscode");
        String hosScheduleId = (String) paramMap.get("hosScheduleId");
        scheduleService.remove(hosCode, hosScheduleId);
        return Result.ok();
    }

    @ApiOperation(value = "查询排班信息")
    @PostMapping("/schedule/list")
    public Result findSchedule(HttpServletRequest request) {
        //获取传递过来科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //获取医院编号和科室编号
        String hosCode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String) paramMap.get("limit"));
        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hosCode);
        scheduleQueryVo.setDepcode(depcode);
        Page<Schedule> pages = scheduleService.findPageSchedule(page, limit, scheduleQueryVo);
        return Result.ok(pages);
    }

    @ApiOperation(value = "上传排班信息")
    @PostMapping("/saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        //获取传递过来科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        scheduleService.save(paramMap);
        return Result.ok();
    }

    @ApiOperation(value = "删除科室信息")
    @PostMapping("/department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        //获取传递过来科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //医院编号 和 科室编号
        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");
        departmentService.remove(hoscode, depcode);
        return Result.ok();
    }

    @ApiOperation(value = "查询科室信息")
    @PostMapping("/department/list")
    public Result findDepartment(HttpServletRequest request) {
        //获取传递过来科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        String hoscode = (String) paramMap.get("hoscode");
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String) paramMap.get("limit"));
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        Page<Department> pages = departmentService.findPageDepartment(page, limit, departmentQueryVo);
        return Result.ok(pages);
    }

    @ApiOperation(value = "上传科室信息")
    @PostMapping("/saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        //获取传递过来科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //获取医院编号
        String hoscode = (String) paramMap.get("hoscode");
        //1 获取医院系统传递过来的签名,签名进行MD5加密
        String hospSign = (String) paramMap.get("sign");
        //2 根据传递过来医院编码，查询数据库，查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3 把数据库查询签名进行MD5加密
        String signKeyMd5 = HttpRequestHelper.getSign(paramMap, signKey);
        //4 判断签名是否一致
        if (!hospSign.equals(signKeyMd5)) {
            throw new EduException(ResultCodeEnum.SIGN_ERROR);
        }
        departmentService.save(paramMap);
        return Result.ok();
    }

    @ApiOperation(value = "上传医院信接口")
    @PostMapping("/saveHospital")
    public Result saveHospital(HttpServletRequest request) {
        //获取传递过来科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //获取医院编号
        String hoscode = (String) paramMap.get("hoscode");
        log.info("医院编号:{}", hoscode);
        //1 获取医院系统传递过来的签名,签名进行MD5加密
        String hospSign = (String) paramMap.get("sign");
        //2 根据传递过来医院编码，查询数据库，查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3.传输过程中“+”转换为了“ ”，因此我们要转换回来
        String logoData = (String) paramMap.get("logoData");
        if(!StringUtils.isEmpty(logoData)) {
            logoData = logoData.replaceAll(" ", "+");
            paramMap.put("logoData", logoData);
        }
        //4.把数据库查询签名进行MD5加密
        String signKeyMd5 = HttpRequestHelper.getSign(paramMap, signKey);
        //5.判断签名是否一致
        if (!hospSign.equals(signKeyMd5)) {
            throw new EduException(ResultCodeEnum.SIGN_ERROR);
        }

        hospitalService.save(paramMap);
        return Result.ok();
    }

    @ApiOperation(value = "查询医院信息")
    @PostMapping("/hospital/show")
    public Result getHospital(HttpServletRequest request) {
        //获取传递过来科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //获取医院编号
        String hoscode = (String) paramMap.get("hoscode");
        //1 获取医院系统传递过来的签名,签名进行MD5加密
        String hospSign = (String) paramMap.get("sign");
        //2 根据传递过来医院编码，查询数据库，查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3 把数据库查询签名进行MD5加密
        String signKeyMd5 = HttpRequestHelper.getSign(paramMap, signKey);
        //4 判断签名是否一致
        if (!hospSign.equals(signKeyMd5)) {
            throw new EduException(ResultCodeEnum.SIGN_ERROR);
        }
        Hospital hospital = hospitalService.getByHospitalCode(hoscode);
        return Result.ok(hospital);
    }
}
